package com.longcai.piaojia.adapter;

import android.app.Activity;
import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.longcai.piaojia.R;
import com.longcai.piaojia.conn.ChooseGoodsListGet;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.bound.BoundViewHelper;
import com.zcx.helper.scale.ScaleScreenHelperFactory;

import java.util.HashMap;
import java.util.List;

/**
 * Created by Administrator on 2015/6/1.
 */
public class ChooseGoodsTimeGridAdapter extends ArrayAdapter<ChooseGoodsListGet.TimeEntity> {

    private LayoutInflater layoutInflater;
    private Context context;

    public HashMap<Integer, Boolean> isChecked = new HashMap<>();

    private List<ChooseGoodsListGet.TimeEntity> objects;

    public ChooseGoodsTimeGridAdapter(Context context, List<ChooseGoodsListGet.TimeEntity> objects) {

        super(context, 0, objects);

        this.context = context;

        this.objects = objects;

        layoutInflater = ((Activity) context).getLayoutInflater();

    }

    public void initCheckFalse(List<ChooseGoodsListGet.TimeEntity> objects, int pos){

        for (int i = 0; i < objects.size(); i++) {

            if(i==pos){

                isChecked.put(i, true);

            }else {

                isChecked.put(i, false);

            }

        }
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder viewHolder;

        ChooseGoodsListGet.TimeEntity timeEntity = getItem(position);

        if (convertView == null) {

            convertView = BoundViewHelper.boundView(viewHolder = new ViewHolder(), ScaleScreenHelperFactory.getInstance().loadView((ViewGroup) layoutInflater.inflate(R.layout.item_choose_goods_time_grid, null)));

            convertView.setTag(viewHolder);

        } else {

            viewHolder = (ViewHolder) convertView.getTag();

        }

        if (isChecked.get(position)){
            viewHolder.item_choose_goods_rl.setBackgroundResource(R.drawable.item_choose_goods_bottom_sel_bg);
            viewHolder.item_choose_goods_time_tv.setTextColor(ContextCompat.getColor(context, R.color.white));
            viewHolder.item_choose_goods_date_tv.setTextColor(ContextCompat.getColor(context, R.color.white));
        }else{
            viewHolder.item_choose_goods_rl.setBackgroundResource(R.drawable.item_choose_goods_bottom_bg);
            viewHolder.item_choose_goods_time_tv.setTextColor(ContextCompat.getColor(context, R.color.text_color));
            viewHolder.item_choose_goods_date_tv.setTextColor(ContextCompat.getColor(context, R.color.text_color));
        }

        viewHolder.item_choose_goods_date_tv.setText(timeEntity.data);
        viewHolder.item_choose_goods_time_tv.setText(timeEntity.week + context.getString(R.string.space) + timeEntity.hour);

        return convertView;

    }

    private class ViewHolder {

        @BoundView(R.id.item_choose_goods_rl) public RelativeLayout item_choose_goods_rl;
        @BoundView(R.id.item_choose_goods_time_tv) public TextView item_choose_goods_time_tv;
        @BoundView(R.id.item_choose_goods_date_tv) public TextView item_choose_goods_date_tv;



    }

}
